package cn.zdq.pictureselector.activity

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.zdq.picture_lib.activity.PictureSelectorActivity
import cn.zdq.picture_lib.config.PictureConfig
import cn.zdq.picture_lib.model.LocalMedia
import cn.zdq.picture_lib.utils.ScreenUtils
import cn.zdq.pictureselector.FullyGridLayoutManager
import cn.zdq.pictureselector.GridSpacingItemDecoration
import cn.zdq.pictureselector.R
import cn.zdq.pictureselector.adapter.GridImageAdapter
import com.permissionx.guolindev.PermissionX

class MainActivity : AppCompatActivity() {
    private var mAdapter: GridImageAdapter? = null

    private lateinit var mRecyclerView: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mRecyclerView = findViewById<RecyclerView>(R.id.recycler)
        val btn1 = findViewById<Button>(R.id.activity_btn)
        btn1.setOnClickListener {

            PermissionX.init(this)
                .permissions(
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                )
                .request { allGranted, grantedList, deniedList ->
                    if (allGranted) {
                        Toast.makeText(this, "All permissions are granted", Toast.LENGTH_LONG)
                            .show()
                        startActivity(
                            Intent(
                                this@MainActivity,
                                PictureSelectorActivity::class.java
                            )
                        )
                    } else {
                        Toast.makeText(
                            this,
                            "These permissions are denied: $deniedList",
                            Toast.LENGTH_LONG
                        ).show()
                    }
                }

        }

//        val manager = GridLayoutManager(
//            this,
//            4, GridLayoutManager.VERTICAL, false
//        )
        val manager = FullyGridLayoutManager(
            this,
            4, GridLayoutManager.VERTICAL, false
        )
        mRecyclerView.setLayoutManager(manager)
        mRecyclerView.addItemDecoration(
            GridSpacingItemDecoration(
                4,
                ScreenUtils.dip2px(this, 8.0f), false
            )
        )
        mAdapter = GridImageAdapter(
            this
        ) {
            startActivityForResult(Intent(this@MainActivity, PictureSelectorActivity::class.java),PictureConfig.CHOOSE_REQUEST)
        }
        mRecyclerView.adapter = mAdapter
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {
                var mediaList = arrayListOf<LocalMedia>()
                if (data != null) {
                    mediaList =
                        data.getParcelableArrayListExtra<LocalMedia>(PictureConfig.EXTRA_RESULT_SELECTION) as ArrayList<LocalMedia>
                }

                mAdapter?.setList(mediaList)
                mAdapter?.notifyDataSetChanged()

            }
        }
    }
}